﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import flash.events.*;
    import flash.display.*;
    import org.aswing.util.*;
    import flash.ui.*;

    public class KeyboardManager extends EventDispatcher {

        protected static var defaultMnemonicModifier:Array = [Keyboard.CONTROL, Keyboard.SHIFT];

        protected var keySequence:ArrayList;
        protected var keymap:KeyMap;
        protected var inited:Boolean;
        protected var mnemonicModifier:Array;
        protected var keyJustActed:Boolean;
        protected var enabled:Boolean;

        public function KeyboardManager(){
            this.enabled = true;
            this.inited = false;
            this.keyJustActed = false;
            this.keySequence = new ArrayList();
            this.keymap = new KeyMap();
            this.mnemonicModifier = null;
        }
        public static function getDefaultMnemonicModifier():Array{
            return (defaultMnemonicModifier.concat());
        }
        public static function setDefaultMnemonicModifier(_arg1:Array):void{
            defaultMnemonicModifier = _arg1.concat();
        }

        public function init(_arg1:DisplayObjectContainer):void{
            if (!this.inited){
                this.inited = true;
                _arg1.addEventListener(KeyboardEvent.KEY_DOWN, this.__onKeyDown, false, 0, true);
                _arg1.addEventListener(KeyboardEvent.KEY_UP, this.__onKeyUp, false, 0, true);
                _arg1.addEventListener(Event.DEACTIVATE, this.__deactived, false, 0, true);
            } else {
                throw (new Error("This KeyboardManager was already inited!"));
            };
        }
        public function registerKeyAction(_arg1:KeyType, _arg2:Function):void{
            this.keymap.registerKeyAction(_arg1, _arg2);
        }
        public function unregisterKeyAction(_arg1:KeyType):void{
            this.keymap.unregisterKeyAction(_arg1);
        }
        public function getKeyMap():KeyMap{
            return (this.keymap);
        }
        public function isKeyDown(_arg1:uint):Boolean{
            return (this.keySequence.contains(_arg1));
        }
        public function setMnemonicModifier(_arg1:Array):void{
            if (_arg1 == null){
                _arg1 = null;
            } else {
                this.mnemonicModifier = _arg1.concat();
            };
        }
        public function setEnabled(_arg1:Boolean):void{
            this.enabled = _arg1;
            if (!_arg1){
                this.keySequence.clear();
            };
        }
        public function isEnabled():Boolean{
            return (this.enabled);
        }
        public function getMnemonicModifier():Array{
            if (this.mnemonicModifier == null){
                return (getDefaultMnemonicModifier());
            };
            return (this.mnemonicModifier.concat());
        }
        public function isMnemonicModifierDown():Boolean{
            var _local1:Array = this.getMnemonicModifier();
            var _local2:int;
            while (_local2 < _local1.length) {
                if (!this.isKeyDown(_local1[_local2])){
                    return (false);
                };
                _local2++;
            };
            return ((_local1.length > 0));
        }
        public function isKeyJustActed():Boolean{
            return (this.keyJustActed);
        }
        private function __onKeyDown(_arg1:KeyboardEvent):void{
            if (!this.enabled){
                return;
            };
            dispatchEvent(_arg1);
            var _local2:uint = _arg1.keyCode;
            if (!this.keySequence.contains(_local2)){
                this.keySequence.append(_local2);
            };
            this.keyJustActed = false;
            if (this.keymap.fireKeyAction(this.keySequence.toArray())){
                this.keyJustActed = true;
            };
        }
        private function __onKeyUp(_arg1:KeyboardEvent):void{
            if (!this.enabled){
                return;
            };
            dispatchEvent(_arg1);
            var _local2:uint = _arg1.keyCode;
            this.keySequence.remove(_local2);
            if (!_arg1.ctrlKey){
                this.keySequence.remove(Keyboard.CONTROL);
            };
            if (!_arg1.shiftKey){
                this.keySequence.remove(Keyboard.SHIFT);
            };
        }
        private function __deactived(_arg1:Event):void{
            this.keySequence.clear();
        }

    }
}//package org.aswing 
